package level_ajax_query.dao;

import level_ajax_query.db.JDBCUtil;
import level_ajax_query.entry.Province;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author Rookie Coder
 * @Description
 * @Date
 **/
public class ProvinceDao {

    /*
     * @Author liu-miss
     * @Description 查找省份
     * @Date 21:33 2021/5/11
     * @Param
     * @return
     **/
    public List<Province> findProvince(){
        JDBCUtil jdbcUtil=new JDBCUtil();
        List<Province> list=null;
        // 创建连接对象
        Connection connection=null;
        // 创建预处理对象
        PreparedStatement pst=null;
        // 创建结果集
        ResultSet rs=null;
        Province province=null;

        try {
            // 获取连接
            connection=jdbcUtil.getConnection();
            // 编写sql
            String sql="select id,name,jiancheng,shenghui from province order by id";
            // 动态编译sql
            pst=connection.prepareStatement(sql);
            // 获取结果集
            rs=pst.executeQuery();
            // 解析结果集
            if (rs!=null){
                list=new ArrayList();
                while (rs.next()){
                    province=new Province();
                    province.setId(rs.getInt("id"));
                    province.setName(rs.getString("name"));
                    province.setJiancheng(rs.getString("jiancheng"));
                    province.setShenghui(rs.getString("shenghui"));
                    list.add(province);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }// 关闭连接
        finally {
            jdbcUtil.closeAll(connection,pst,rs);
        }
            return list;
    }

}
